<?php

//聊吧收藏功能

namespace Account\Controller;

use Common\Controller\ApiController;
use Util\ModelUtils;

class MarkController extends ApiController {
    /*
     * 添加收藏
     */
    protected $user_field = ['user_id', 'face', 'nickname', 'sex', 'token','intro'];
    
    public function store() {
        $input = $this->request();
        $uid = $this->user['id'];
        $input['user_id'] = $uid;
        $this->checkNull($input, ['type']);
//        if (!empty($input['img'])) {
//            $input['img'] = uploadBase64toOss($input['img'], 'mark_' . $uid . '_' . time());
//        }
        $input['create_time'] = time();
        $id = D('Mark')->add($input);
        if ($id) {
            $this->createdResponse();
        }
        $this->errorResponse(400, "收藏失败");
    }

    /*
     * 修改和删除
     */

    public function update() {
        $input = $this->request();
        if (!empty($input['delete_time'])) {
            $input['delete_time'] = time();
        }
        D('Mark')->save($input);
        $this->createdResponse();
    }

    public function search() {
        $input = $this->request();
        $page = filter($input['page'], 1);
        $perPage = filter($input['per_page'], 30);
        $uid = $this->user['id'];
        $markM = D('Mark');
        $where['delete_time'] = 0;
        $where['user_id'] = $uid;
        if ($input['keyword']) {
            $where['text'] = ['like', '%' . $input['keyword'] . '%'];
        }
        $count = $markM->where($where)->count();
        $mark = [];
        if ($count > 0) {
            $mark = $markM->where($where)
                    ->order('create_time desc')
                    ->page($page, $perPage)
                    ->select();
        }
//        $mark = transform('Mark', 'pub', $mark);
        ModelUtils::belongsTo($mark, ['foreignKey' => 'to_user_id', 'relation' => 'Account/Users', 'refKey' => 'user_id','fields' => $this->user_field, 'name' => 'author']);
        $pagination = pagination($count, $perPage, $page);
        $this->response($mark, $pagination);
    }

}
